package com.hctg.forced2sleep.utils;

import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.hctg.forced2sleep.databinding.LayoutBottomBarViewBinding;
import com.luck.picture.lib.interfaces.OnItemClickListener;

/**
 * description: 描述
 * company    :
 *
 * @author : ldf
 * date       : 2024/6/9 on 21
 * @version 1.0
 */
public class MainBottomBarHelper {

    private LayoutBottomBarViewBinding viewBinding;
    private OnItemClickListener onItemClickListener;

    public void init(ViewPager viewPager, LayoutBottomBarViewBinding viewBinding) {
        this.viewBinding = viewBinding;
        viewBinding.stvHome.setSelected(true);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                setSelectedItem(null, position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        //首页
        viewBinding.stvHome.setOnClickListener(view -> {
            this.onItemClickListener.onItemClick(view, 0);
            setSelectedItem(view, 0);
        });
        //星球
        viewBinding.stvPlanet.setOnClickListener(view -> {
            this.onItemClickListener.onItemClick(view, 1);
            setSelectedItem(view, 1);
        });
        //???
        viewBinding.stvCelestialBody.setOnClickListener(view -> {
            this.onItemClickListener.onItemClick(view, 2);
            setSelectedItem(view, 2);
        });
        //统计
        viewBinding.stvStatistics.setOnClickListener(view -> {
            this.onItemClickListener.onItemClick(view, 3);
            setSelectedItem(view, 3);
        });
        //我的
        viewBinding.stvMine.setOnClickListener(view -> {
            this.onItemClickListener.onItemClick(view, 4);
            setSelectedItem(view, 4);
        });
    }

    private void setSelectedItem(View view, int itemPos) {
        viewBinding.stvHome.setSelected(itemPos == 0);
        viewBinding.stvPlanet.setSelected(itemPos == 1);
        viewBinding.stvCelestialBody.setSelected(itemPos == 2);
        viewBinding.stvStatistics.setSelected(itemPos == 3);
        viewBinding.stvMine.setSelected(itemPos == 4);

    }
}
